home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Pascal / Snippets / AntiAlias II / AntiAlias.p < prev    next >
Encoding:
Text File  |  1995-12-02  |  4.8 KB  |  174 lines  |  [TEXT/PJMM]

  1. {Based on a demo by oster@netcom.com (David Phillip Oster),}
  2. {posted on alt.sources.mac in 1994.}
  3.  
  4. {*******************************}
  5. {This version by Ingemar Ragnemalm.}
  6. {Uses an offscreen with selectable depth. With depth 1, as in the original program,}
  7. {I didn't manage to get any anti-aliasing at all. However, with depth 2, it changes}
  8. {altogether. Modify the kOffDepth constant below to try it.}
  9. {*******************************}
  10.  
  11. unit AntiAlias;
  12.  
  13. interface
  14.     uses
  15. {$IFC UNDEFINED THINK_PASCAL}
  16.         Types, QuickDraw, ToolUtils, Events, Menus, Dialogs, Fonts, Resources, Devices, 
  17. {$ENDC}
  18.         QDOffscreen;
  19.  
  20.     function ADrawString (text: Str255): OSErr;
  21.     function ADrawText (text: Ptr; off, len: integer): OSErr;
  22.     function ADrawPicture (ph: PicHandle; where: Rect): OSErr;
  23.  
  24. implementation
  25.  
  26.     const
  27. {Offscreen Depth. Originally 1. Set to 2 or more for a much nicer result. Set to more if you use color!}
  28.         kOffDepth = 8;
  29.  
  30.  
  31. (* ADrawString - draw a string using copybits antialiasing *)
  32.     function ADrawString (text: Str255): OSErr;
  33.     begin
  34.         ADrawString := ADrawText(@text, 1, ord(text[0]));
  35.     end;
  36.  
  37. (* ADrawText - draw a text using copybits antialiasing *)
  38.     function ADrawText (text: Ptr; off, len: integer): OSErr;
  39.         var
  40.             errCode: OSErr;
  41.             savePort: CGrafPtr;
  42.             saveDev: GDHandle;
  43.             bigGWorld: GWorldPtr;
  44.             curPort: GrafPtr;
  45.             fInfo: FontInfo;
  46.             theTextFace: Style;
  47.             bwRect, destRect: Rect;
  48.             theTextSize, theTextFont, width, height, smallAscent: Integer;
  49.     begin
  50.         GetPort(curPort);
  51.         GetFontInfo(fInfo);
  52.  
  53.         smallAscent := fInfo.ascent;
  54.         theTextFace := curPort^.txFace;
  55.         theTextFont := curPort^.txFont;
  56.         theTextSize := curPort^.txSize;
  57.         TextSize(theTextSize * 4);
  58.  
  59.         width := TextWidth(text, off, len);
  60.         width := width + 32 - width mod 32;
  61.  
  62.         GetFontInfo(fInfo);
  63.         height := fInfo.ascent + fInfo.descent;
  64.         height := height + 4 - height mod 4;
  65.  
  66.         TextSize(theTextSize);
  67.  
  68.         bwRect.left := 0;
  69.         bwRect.top := 0;
  70.         bwRect.right := width;
  71.         bwRect.bottom := height;
  72.  
  73.         destRect := bwRect;
  74.         destRect.right := destRect.right div 4;
  75.         destRect.bottom := destRect.bottom div 4;
  76.  
  77.         OffsetRect(destRect, curPort^.pnLoc.h, curPort^.pnLoc.v - smallAscent);
  78.  
  79. {If I use a bigger depth than 1 for the GWorld below, the result is much nicer! But slower!}
  80. {$IFC UNDEFINED THINK_PASCAL}
  81.         errCode := NewGWorld(bigGWorld, kOffDepth, bwRect, nil, nil, 0);
  82.         if noErr <> errCode then
  83.             errCode := NewGWorld(bigGWorld, kOffDepth, bwRect, nil, nil, useTempMem);
  84. {$ELSEC}
  85.         errCode := NewGWorld(bigGWorld, kOffDepth, bwRect, nil, nil, []);
  86.         if noErr <> errCode then
  87.             errCode := NewGWorld(bigGWorld, kOffDepth, bwRect, nil, nil, [useTempMem]);
  88. {$ENDC}
  89.         if noErr <> errCode then
  90.             begin
  91.                 ADrawText := errCode;
  92.                 exit(ADrawText);
  93.             end;
  94.  
  95.         if LockPixels(GetGWorldPixMap(bigGWorld)) then
  96.             ;
  97.  
  98.         GetGWorld(savePort, saveDev);
  99.         SetGWorld(bigGWorld, nil);
  100.  
  101.         EraseRect(bwRect);
  102.  
  103.         TextSize(theTextSize * 4);
  104.         TextFont(theTextFont);
  105.         TextFace(theTextFace);
  106.  
  107.         MoveTo(0, fInfo.ascent);
  108.         DrawText(text, off, len);
  109.  
  110.         SetGWorld(savePort, saveDev);
  111.  
  112.         CopyBits(GrafPtr(bigGWorld)^.portBits, curPort^.portBits, bwRect, destRect, BitOr(ditherCopy, curPort^.txMode), nil);
  113.         Move(destRect.right - destRect.left, 0);
  114.  
  115.         UnlockPixels(GetGWorldPixMap(bigGWorld));
  116.         DisposeGWorld(bigGWorld);
  117.  
  118.         ADrawText := errCode;
  119.     end; {ADrawString}
  120.  
  121. (* ADrawPicture - draw a picture using copybits antialiasing *)
  122.     function ADrawPicture (ph: PicHandle; where: Rect): OSErr;
  123.         var
  124.             errCode: OSErr;
  125.             savePort: CGrafPtr;
  126.             saveDev: GDHandle;
  127.             bigGWorld: GWorldPtr;
  128.             curPort: GrafPtr;
  129.             bigRect: Rect;
  130.     begin
  131.         GetPort(curPort);
  132.  
  133.         bigRect := where;
  134.         OffsetRect(bigRect, -bigRect.left, -bigRect.top); {Align to (0,0)}
  135.         bigRect.right := bigRect.right * 2;
  136.         bigRect.bottom := bigRect.bottom * 2;
  137.  
  138. {If I use a bigger depth than 1 for the GWorld below, the result is much nicer! But slower!}
  139. {Note! You must, of course, use a bit depth big enough for the colors you have!}
  140. {$IFC UNDEFINED THINK_PASCAL}
  141.         errCode := NewGWorld(bigGWorld, kOffDepth, bigRect, nil, nil, 0);
  142.         if noErr <> errCode then
  143.             errCode := NewGWorld(bigGWorld, kOffDepth, bigRect, nil, nil, useTempMem);
  144. {$ELSEC}
  145.         errCode := NewGWorld(bigGWorld, kOffDepth, bigRect, nil, nil, []);
  146.         if noErr <> errCode then
  147.             errCode := NewGWorld(bigGWorld, kOffDepth, bigRect, nil, nil, [useTempMem]);
  148. {$ENDC}
  149.         if noErr <> errCode then
  150.             begin
  151.                 ADrawPicture := errCode;
  152.                 exit(ADrawPicture);
  153.             end;
  154.  
  155.         if LockPixels(GetGWorldPixMap(bigGWorld)) then
  156.             ;
  157.  
  158.         GetGWorld(savePort, saveDev);
  159.         SetGWorld(bigGWorld, nil);
  160.  
  161.         EraseRect(bigRect);
  162.         DrawPicture(ph, bigRect);
  163.  
  164.         SetGWorld(savePort, saveDev);
  165.  
  166.         CopyBits(GrafPtr(bigGWorld)^.portBits, curPort^.portBits, bigRect, where, BitOr(ditherCopy, curPort^.txMode), nil);
  167.  
  168.         UnlockPixels(GetGWorldPixMap(bigGWorld));
  169.         DisposeGWorld(bigGWorld);
  170.  
  171.         ADrawPicture := errCode;
  172.     end; {ADrawPicture}
  173.  
  174. end.